/*************************************************************************
 *  Copyright © 2023-2030 FXB CO.,LTD. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：CustomBuildProcess
 *  文件：BuildInfoConfig.cs
 *  作者：Administrator
 *  日期：2024/11/21 16:24:58
 *  功能：Nothing
*************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace CustomBuildProcess.Editor
{
    [Serializable]
    public class BuildInfo
    {
        public string buildTime;
        public string appID;
        public string platform;
        public string feature;
        public string model;
        public string language;
        public string type;
        public string companyName;
        public string productName;
        public string version;
        public Texture2D defaultIcon;
        [Multiline]
        public string log;
    }

    public class BuildInfoConfig : ScriptableObject
    {
        public List<BuildInfo> buildInfos = new List<BuildInfo>();

        public void AddBuildInfo(BuildInfo buildInfo)
        {
            buildInfos.Add(buildInfo);
        }

        public void RemoveBuildInfo(string version, string type)
        {
            BuildInfo info = GetBuildInfo(version, type);
            if (info != null)
                buildInfos.Remove(info);
        }

        public BuildInfo GetBuildInfo(int index)
        {
            return buildInfos[index];
        }

        public BuildInfo GetBuildInfo(string version,string type)
        {
            return buildInfos.Find(info => info.version == version && info.type == type);
        }

        public string GetBuildLog(string version)
        {
            BuildInfo buildInfo = buildInfos.Find(info => info.version == version);
            if (buildInfo != null)
                return buildInfo.log;
            return null;
        }

        public string GetBuildType(string version)
        {
            BuildInfo buildInfo = buildInfos.Find(info => info.version == version);
            if (buildInfo != null)
                return buildInfo.type;
            return null;
        }

        public bool IsExistVersion(string version)
        {
            return buildInfos.FirstOrDefault(item => item.version == version) != null;
        }

        public BuildInfo GetLastVersionBuildInfo(out int index)
        {
            index = buildInfos.Count > 0 ? buildInfos.Count - 1 : 0;
            return buildInfos.Count > 0 ? buildInfos.LastOrDefault() : null;
        }

        public string GetVersion(int index)
        {
            return buildInfos[index].version;
        }

        public string[] GetAllVersions()
        {
            List<string> versions = new List<string>();
            foreach (var info in buildInfos)
            {
                versions.Add(info.version);
            }
            return versions.ToArray();
        }

        public string[] GetAllVersionsDisplayName()
        {
            List<string> versions = new List<string>();
            foreach (var info in buildInfos)
            {
                versions.Add(info.version + " (" + info.type + ")");
            }
            return versions.ToArray();
        }

        public int GetAllVersionCount()
        {
            return buildInfos.Count;
        }
    }
}